package com.brkj.communityStudy;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.brkj.communityStudy.model.DS_QAComment;
import com.brkj.httpInterface.BaseAjaxParams;
import com.brkj.httpInterface.HttpInterface;
import com.brkj.httpInterface.NomalResult;
import com.brkj.main.R;
import com.brkj.util.ImgShow;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.util.MyApplication;
import com.brkj.util.view.BaseCoreActivity;
import com.brkj.util.view.CommonDialog;
import com.brkj.util.view.CommonDialog2Btn;
import com.dgl.sdk.view.CircleImageView;
import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;

import java.util.List;

public class QACommentAdapter extends BaseAdapter{
	private ListItemView listItemView;
	protected LayoutInflater listContainer;
	private Context context;
	private List<DS_QAComment> commentList;
	private FinalBitmap fb;
	private Bitmap failBitmap;
    private boolean isShowSelectBest;
    private int  questionID;

	public QACommentAdapter(Context context, List<DS_QAComment> commentList, int questionID, boolean isShowSelectBest){
		this.context = context;
		this.commentList = commentList;
		this.questionID = questionID;
		this.isShowSelectBest = isShowSelectBest;


		listContainer = LayoutInflater.from(context);
		fb = FinalBitmap.create(context);
		
		failBitmap =
				BitmapFactory.decodeResource(MyApplication.getContext().getResources(), R.drawable.note_head_small);
	}
	
	private static class ListItemView{
		public CircleImageView UserHeadImg;
		public TextView Name;
		public TextView Content;
		public TextView Time;
		public TextView selectAsBest;
		public TextView upNum;
		public View verLine;
		public View upView;
		public ImageView isBest;
	};
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return commentList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// 自定义视图
		listItemView = new ListItemView();
		// 获取list_item布局文件的视图
		if (convertView == null) {
			convertView = listContainer.inflate(R.layout.qa_comment_item, null);
			// 获取控件对象
			listItemView.UserHeadImg = (CircleImageView) convertView.findViewById(R.id.UserHeadImg);
			listItemView.Name = (TextView) convertView
					.findViewById(R.id.Name);
			listItemView.Content = (TextView) convertView
					.findViewById(R.id.Content);
			listItemView.Time = (TextView) convertView
					.findViewById(R.id.Time);
            listItemView.selectAsBest = (TextView) convertView
					.findViewById(R.id.selectAsBest);
            listItemView.upNum = (TextView) convertView
					.findViewById(R.id.upNum);
            listItemView.verLine = convertView
					.findViewById(R.id.verLine);
            listItemView.upView = convertView
					.findViewById(R.id.upView);
            listItemView.isBest = (ImageView) convertView
					.findViewById(R.id.isBest);

			// 设置控件集到convertView
			convertView.setTag(listItemView);
		} else {
			listItemView = (ListItemView) convertView.getTag();
		}

		String url  = commentList.get(position).getUserHeadImg().replace('\\', '/');
		
//		fb.display(listItemView.CourseImg, commentList.get(position).getCourseImg());
		ImgShow.display(listItemView.UserHeadImg, url, R.drawable.note_head_small);
		
//		ImageLoader.getInstance().displayImage(
//				"http://118.192.9.79:7002/KMELearning//WebFiles/cwFiles/52266A266-BBFF-4482-A6A7-02196AB18763k.jpg"
//				, listItemView.CourseImg);
		listItemView.Name.setText(commentList.get(position).getName());
		listItemView.Content.setText(commentList.get(position).getAnswerContent());
		listItemView.Time.setText(commentList.get(position).getTime());


        if(commentList.get(position).getIsBest() == 0){
            listItemView.isBest.setVisibility(View.GONE);
        }else{
            listItemView.isBest.setVisibility(View.VISIBLE);
        }

        listItemView.upNum.setText(commentList.get(position).getUpNum()+"");

        listItemView.upView.setTag(position);
        listItemView.upView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = (Integer)v.getTag();

                ((TextView)(v.findViewById(R.id.upNum))).
                        setText(commentList.get(pos).getUpNum() + 1 + "");
                v.setOnClickListener(null);

                BaseAjaxParams params = new BaseAjaxParams();
                params.put(HttpInterface.HIF_UpAnswer.params.QuestionID, Integer.toString(questionID));
                params.put(HttpInterface.HIF_UpAnswer.params.answerID,
                        Integer.toString(commentList.get(pos).getAnswerID()));

                FinalHttp fh = new FinalHttp();
                fh.post(HttpInterface.HIF_UpAnswer.address, params, new AjaxCallBack<Object>() {
                    @Override
                    public void onSuccess(Object o) {
                        super.onSuccess(o);
                        if(NomalResult.doResult(o, context)){
//                            ((BaseCoreActivity)context).showToast("点赞成功");
                        }else{
//                            ((BaseCoreActivity)context).showToast("点赞失败");
                        }
                    }
                });
            }
        });

        if(isShowSelectBest){
            listItemView.selectAsBest.setVisibility(View.VISIBLE);
        }else{
            listItemView.selectAsBest.setVisibility(View.GONE);
        }

        if(isShowSelectBest){
            convertView.setOnClickListener(new MyOnclickListener(position));
        }

		return convertView;
	}


    class MyOnclickListener implements View.OnClickListener{
        private int position;

        public MyOnclickListener(int position){
            this.position = position;
        }

        @Override
        public void onClick(View v) {

            CommonDialog2Btn dialog = new CommonDialog2Btn(context, "选择此回答为最佳答案？", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BaseAjaxParams params = new BaseAjaxParams();
                    params.put(HttpInterface.HIF_SelectBestAnswer.params.QuestionID, Integer.toString(questionID));
                    params.put(HttpInterface.HIF_SelectBestAnswer.params.answerID,
                            Integer.toString(commentList.get(position).getAnswerID()));

                    FinalHttp fh = new FinalHttp();
                    fh.post(HttpInterface.HIF_SelectBestAnswer.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)context) {
                        @Override
                        public void onSuccess(Object o) {
                            super.onSuccess(o);
                            if(NomalResult.doResult(o, context)){
                                commentList.get(position).setIsBest(1);
                                notifyDataSetChanged();
                                ((BaseCoreActivity) context).showToast("选为最佳答案成功");
                            }
                        }
                    });
                }
            });


            dialog.show();
        }
    }
}
